import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:thinking/config/_.dart';
import 'package:thinking/entity/_.dart';
import 'package:thinking/service/_.dart';
import 'package:thinking/utils/_.dart';
import 'package:thinking/widget/_.dart';

class SearchTopicValue extends StatefulWidget {
  final String value;
  SearchTopicValue({required this.value,});
  @override
  _SearchTopicValue createState() =>
      _SearchTopicValue(value: value);
}

class _SearchTopicValue extends State<SearchTopicValue>
    with AutomaticKeepAliveClientMixin<SearchTopicValue> {
  final String value;
  List<Tag> tagData = [];
  late PageEntity _pageEntity;
  late EasyRefreshController _easyRefreshController;
  late Future<dynamic> _function;

  _SearchTopicValue({required this.value});

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    _pageEntity = PageEntity();
    _easyRefreshController = EasyRefreshController();
    _function = future();
  }

  future() async {
    await getValue();
  }

  Future<void> getValue() async {
    var res = (await SearchService.getSearchTopicData(params: {
      "search_Value": value,
      "search_Page": _pageEntity.page,
      "search_count": _pageEntity.count,
      "user_Id": SPreferences().getInt('user_Id')
    }))!;
    if (!_pageEntity.canLoad) {
      BotToast.showText(text: '没有更多话题了哦~');
    } else {
      BotToast.showText(text: '更新了${res.length}条话题~');
    }
    if (res.length < _pageEntity.count && _pageEntity.canLoad) {
      _pageEntity.setPage = _pageEntity.page + 1;
      _pageEntity.setCanLoad = false;
    }
    tagData = [...tagData, ...res];
    setState(() {});
  }

  @override
  void dispose() {
    _easyRefreshController.dispose();
    super.dispose();
  }

  Widget easyRefresh(Widget child) {
    return EasyRefresh(
      child: child,
      header: BezierCircleHeader(backgroundColor: ColorConfig.ThemeColor),
      footer: BezierBounceFooter(backgroundColor: ColorConfig.ThemeColor),
      enableControlFinishRefresh: false,
      enableControlFinishLoad: true,
      controller: _easyRefreshController,
      bottomBouncing: true,
      onLoad: () async {
        if (_pageEntity.canLoad) {
          _pageEntity.setPage = _pageEntity.page + 1;
        }

        await getValue();
        _easyRefreshController.finishLoad(success: true);
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return FutureBuilder(
        future: _function,
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          return snapshot.connectionState == ConnectionState.done
              ? easyRefresh(Empty(
                  child: ListView(
                    shrinkWrap: true,
                    physics: BouncingScrollPhysics(),
                    children: tagData
                        .map((e) => GestureDetector(
                      onTap: (){

                        Modular.to.pushNamed('/topicIndex/${e.id}/${e.tagTitle}/${e.isSub!?1:0}');
                      },
                      child: TopicItemWidget(
                        isShowButton: true,
                        tagData: e,
                        type: 1,
                        key: ValueKey(e.id),
                      ),
                    ))
                        .toList(),
                  ),
                  isShow: tagData.length == 0,
                ))
              : Loading(
                  height: 300,
                );
        });
  }
}
